package viewer;

import client.IOCLient;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import model.Flight;
import сontrol.*;

/*
 * NewJFrame.java
 *
 * Created on 03.11.2011, 9:28:50
 */
/**
 *
 * @author Admin
 */
public class InputWindow extends javax.swing.JFrame {

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JCheckBox jCheckBox7;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JSpinner jSpinner3;
    private javax.swing.JSpinner jSpinner4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
    private static MainWindow jfr;
    private static int selected = 0;

    /** Creates new form Inp */
    public InputWindow() throws IOException {
        initComponents();
        /**  Ссылка на открывшее окно */
        InputWindow.jfr = new MainWindow();
    }

    /**
     * Конструктор для инициализации формы ввода
     * @param jfr Объект, вызвавшей формы
     */
    public InputWindow(MainWindow jfr) throws FileNotFoundException, IOException {

        initComponents();
        InputWindow.jfr = jfr;
        getListItem();
        /** Изменение вида окна */
        if (MainWindow.openKind == 1) {
            this.jButton3.setVisible(true);
            this.jTextField1.setEditable(true);
            this.jCheckBox1.setVisible(true);
            this.jCheckBox2.setVisible(true);
            this.jCheckBox3.setVisible(true);
            this.jCheckBox4.setVisible(true);
            this.jCheckBox5.setVisible(true);
            this.jCheckBox6.setVisible(true);
            this.jCheckBox7.setVisible(true);
        } else {
            this.jButton3.setVisible(false);
            this.jTextField1.setEditable(true);
            this.jCheckBox1.setVisible(false);
            this.jCheckBox2.setVisible(false);
            this.jCheckBox3.setVisible(false);
            this.jCheckBox4.setVisible(false);
            this.jCheckBox5.setVisible(false);
            this.jCheckBox6.setVisible(false);
            this.jCheckBox7.setVisible(false);
            if (MainWindow.openKind == 2) {
                /** получение значений на форму из редактируемой строки таблицы */
                this.jButton3.setVisible(false);
                this.jTextField1.setEditable(false);
                DefaultTableModel mod = (DefaultTableModel) jfr.jTable1.getModel();
                Vector<String> v = (Vector<String>) mod.getDataVector().elementAt(jfr.jTable1.getSelectedRow());
                this.jTextField1.setText(v.get(0));
                this.jTextField2.setText(v.get(1));
                String a = v.get(2);
                String c = a.substring(0, a.indexOf("/"));
                this.jComboBox1.setSelectedItem(c);
                c = a.substring(a.indexOf("/") + 1, a.lastIndexOf("/"));
                this.jComboBox2.setSelectedItem(c);

                a = v.get(3);
                c = a.substring(0, a.indexOf("/"));
                this.jComboBox3.setSelectedItem(c);
                c = a.substring(a.indexOf("/") + 1, a.lastIndexOf("/"));
                this.jComboBox4.setSelectedItem(c);

                String[] split = v.get(4).split(":");
                this.jSpinner1.setValue(Integer.parseInt(split[0]));
                this.jSpinner2.setValue(Integer.parseInt(split[1]));

                split = v.get(5).split(":");
                this.jSpinner3.setValue(Integer.parseInt(split[0]));
                this.jSpinner4.setValue(Integer.parseInt(split[1]));
            }
        }
    }

    /**
     * Метод,добавляющий строчку к таблице
     * @param str Строка таблицы
     */
    public void add(Vector str) {
        DefaultTableModel mod = (DefaultTableModel) jfr.jTable1.getModel();
        mod.addRow(str);
    }
    /**
     * Метод,очищающий таблицу и заполняющий ее новыми значениями
     * @param table Список строк таблицы
     */
     public static void fill(ArrayList<Vector> table,MainWindow jfr) {
        DefaultTableModel mod = (DefaultTableModel) jfr.jTable1.getModel();
        mod.getDataVector().removeAllElements();
        if (table != null) {
            for (Vector x : table) {
                mod.addRow(x);
            }
        }
        jfr.jTable1.updateUI();
    }
    /**
     * Метод,редактирующий изменяемую строку
     * @param index Номер изменяемой строки
     *@param vect Изменяемая строка таблицы
     */
    public static void updateFlight(Vector vect) {
        DefaultTableModel mod = (DefaultTableModel) jfr.jTable1.getModel();
        if (jfr.jTable1.getSelectedRow() != -1) {
            selected = jfr.jTable1.getSelectedRow();
        }
        mod.removeRow(selected);
        mod.insertRow(selected, vect);
        jfr.jTable1.updateUI();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jSpinner1 = new javax.swing.JSpinner();
        jSpinner2 = new javax.swing.JSpinner();
        jSpinner3 = new javax.swing.JSpinner();
        jSpinner4 = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jCheckBox7 = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setResizable(false);

        jButton1.setText("ОК");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("№ рейса");

        jLabel2.setText("Аэробус");

        jLabel3.setText("Пункт вылета");

        jLabel5.setText("Время вылета");

        jLabel6.setText("Путевое время");

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(0, 0, 24, 1));
        jSpinner1.setEditor(new javax.swing.JSpinner.NumberEditor(jSpinner1, ""));

        jSpinner2.setModel(new javax.swing.SpinnerNumberModel(0, 0, 60, 1));

        jSpinner3.setModel(new javax.swing.SpinnerNumberModel(0, 0, 24, 1));

        jSpinner4.setModel(new javax.swing.SpinnerNumberModel(0, 0, 60, 1));

        jLabel7.setText("Пункт прибытия");

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jCheckBox4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBox4MouseClicked(evt);
            }
        });

        jCheckBox6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBox6MouseClicked(evt);
            }
        });

        jLabel4.setText("Дополнительная информация:");

        jTextField3.setEditable(false);
        jTextField3.setHorizontalAlignment(javax.swing.JTextField.LEFT);

        jButton2.setText("CLOSE");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Очистить");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel7))
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField2)
                                    .addComponent(jComboBox3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBox1, 0, 220, Short.MAX_VALUE)
                                    .addComponent(jTextField1)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBox1)
                                    .addComponent(jCheckBox5)
                                    .addComponent(jCheckBox3)
                                    .addComponent(jCheckBox2)
                                    .addComponent(jCheckBox4)
                                    .addComponent(jCheckBox6)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(18, 18, 18))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(23, 23, 23)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jSpinner4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton2))
                                    .addComponent(jButton3, 0, 0, Short.MAX_VALUE))))
                        .addContainerGap(36, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(249, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(19, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jCheckBox1)))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox4))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox5)
                        .addGap(6, 6, 6)
                        .addComponent(jCheckBox6)))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSpinner4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1)
                                .addComponent(jButton2))
                            .addComponent(jCheckBox7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Инициализация выпадающих списков городов и аэропортов */
    private void getListItem() throws IOException {
        String s = "";
        ArrayList<String> list = new ArrayList<String>();
        FileInputStream fis = new FileInputStream("air.txt");
        InputStreamReader isr = new InputStreamReader(fis, "windows-1251");
        BufferedReader br = new BufferedReader(isr);
        while (br.ready()) {
            s = br.readLine();
            if (!list.contains(s)) {
                list.add(s);
            }
            br.readLine();
            br.readLine();
        }
        for (String x : list) {
            this.jComboBox1.addItem(x);
            this.jComboBox3.addItem(x);
        }
    }

    /**
     * Выполнение добавлений,удаления или поиска в зависимости от вызова
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Boolean checkSearch = true;
        jTextField3.setText("");
        OperationResult execute;
        Flight flight = new Flight();
        String flightN = jTextField1.getText();
        int flightsNumber = 0;
        String aerobus = jTextField2.getText();
        String depCity = jComboBox1.getSelectedItem().toString();
        String depAir = jComboBox2.getSelectedItem().toString();
        String arrCity = jComboBox3.getSelectedItem().toString();
        String arrAir = jComboBox4.getSelectedItem().toString();
        String depHour = jSpinner1.getValue().toString();
        String depMin = jSpinner2.getValue().toString();
        String travelHour = jSpinner3.getValue().toString();
        String travelMin = jSpinner4.getValue().toString();

        Date departTime = Convertor.toDate(depHour, depMin);
        Date travelTime = Convertor.toDate(travelHour, travelMin);

        execute = HlpController.strToInt(flightN);
        if (execute.getErrorCode() < 0) {
            checkSearch = false;
        } else {
            flightsNumber = Integer.parseInt(flightN);
        }
        flight = Convertor.convertToFlight(flightsNumber, aerobus,
                depCity, depAir, arrCity, arrAir,
                departTime, travelTime);
        switch (MainWindow.openKind) {

            //добавление
            case 0:
                execute = HlpController.execute(HlpController.Instructions.ADD, flight, jfr.flights);
                if (execute.getErrorCode() < 0) {
                    jTextField3.setText(execute.geterrorMessage());
                } else {
                    jfr.flights = IOCLient.work(flight, "add", MainWindow.nameId);
                    add(Convertor.ToVector(flight));
                }
                break;

            //поиск
            case 1:
                if (!this.jCheckBox1.isSelected()) {
                    flightsNumber = -1;
                    checkSearch = true;
                } else {
                    execute = HlpController.strToInt(flightN);
                    if (execute.getErrorCode() < 0) {
                        jTextField3.setText(execute.geterrorMessage());
                    }
                }
                if (!this.jCheckBox2.isSelected()) {
                    aerobus = null;
                }
                if (!this.jCheckBox3.isSelected()) {
                    depCity = null;
                }
                if (!this.jCheckBox4.isSelected()) {
                    depAir = null;
                }
                if (!this.jCheckBox5.isSelected()) {
                    arrCity = null;
                }
                if (!this.jCheckBox6.isSelected()) {
                    arrAir = null;
                }
                if (!this.jCheckBox7.isSelected()) {
                    departTime = null;
                }
                travelTime = null;
                flight = Convertor.convertToFlight(flightsNumber, aerobus,
                        depCity, depAir, arrCity, arrAir,
                        departTime, travelTime);
                if (checkSearch) {
                    jfr.flightsSearch = IOCLient.search(flightsNumber, aerobus, arrCity, arrAir, depCity, depAir, departTime, MainWindow.nameId);
                    fill(Convertor.ToArrayList(jfr.flightsSearch),jfr);
                }
                break;

            // редактирование
            case 2:
                execute = HlpController.execute(HlpController.Instructions.UPDATE, flight, jfr.flights);
                if (execute.getErrorCode() < 0) {
                    jTextField3.setText(execute.geterrorMessage());
                } else {
                    jfr.flights = IOCLient.work(flight, "update", MainWindow.nameId);
                    fill(Convertor.ToArrayList(jfr.flights),jfr);
                }
                break;
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * Изменение списка аэропортов при изменении города отправления
     */
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed

        ArrayList<String> arr = null;
        try {
            arr = item("air.txt", jComboBox1.getSelectedItem().toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        DefaultComboBoxModel as = new DefaultComboBoxModel(arr.toArray());
        jComboBox2.setModel(as);
    }//GEN-LAST:event_jComboBox1ActionPerformed
    /**
     * Изменение списка аэропортов при изменении города прибытия
     */
    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed
        ArrayList<String> arr = null;
        try {
            arr = item("air.txt", jComboBox3.getSelectedItem().toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        DefaultComboBoxModel as = new DefaultComboBoxModel(arr.toArray());
        jComboBox4.setModel(as);// TODO add your handling code here:
    }//GEN-LAST:event_jComboBox3ActionPerformed
    /**
     * Запрет на поиск по аэропорту безотносительно к городу отправления
     */
    private void jCheckBox4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBox4MouseClicked
        if (this.jCheckBox4.isSelected()) {
            this.jCheckBox3.setSelected(true);
        }
    }//GEN-LAST:event_jCheckBox4MouseClicked
    /**
     * Запрет на поиск по аэропорту безотносительно к городу прибытия
     */
    private void jCheckBox6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBox6MouseClicked
        if (this.jCheckBox6.isSelected()) {
            this.jCheckBox5.setSelected(true);
        }
    }//GEN-LAST:event_jCheckBox6MouseClicked
    /**
     * Закрытие формы
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        fill(Convertor.ToArrayList(jfr.flights),jfr);
    }//GEN-LAST:event_jButton3ActionPerformed
    /**
     * Получение списка аэропортов в городе по выбранному городу
     * @param filename Имя файла, хранящего список аэропортов
     * @param city Название города
     * @return Список аэропортов в городе
     */
    private ArrayList item(String filename, String city) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        ArrayList<String> arr = new ArrayList<String>();
        FileInputStream fis = new FileInputStream(filename);
        InputStreamReader isr = new InputStreamReader(fis, "windows-1251");
        BufferedReader br = new BufferedReader(isr);

        while (br.ready()) {
            if (br.readLine().contains(city)) {
                arr.add(br.readLine() + " " + br.readLine());
            }
        }
        br.close();
        isr.close();
        fis.close();
        return arr;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new InputWindow().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}
